<template>
  <div class="product-table">
    <el-table :data="tableData" border style="width: 100%; margin-top: 20px;">
      <el-table-column prop="name" label="商品名称">
        <template slot-scope="scope">
          <span v-if="table === 'bijiben'">{{scope.row.bijibenming}}</span>
          <span v-if="table === 'diannaozhuji'">{{scope.row.zhujiming}}</span>
          <span v-if="table === 'yingjianzuzhuang'">{{scope.row.yingjianming}}</span>
          <span v-if="table === 'diannaowaishe'">{{scope.row.waishemingcheng}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="pinpai" label="品牌"></el-table-column>
      <el-table-column prop="jiage" label="价格"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
          <el-tag v-if="scope.row.status === 1" type="success">已通过</el-tag>
          <el-tag v-if="scope.row.status === 2" type="danger">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <el-image 
            style="width: 50px; height: 50px"
            :src="scope.row.tupian"
            :preview-src-list="[scope.row.tupian]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  props: {
    table: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      tableData: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      const params = {
        page: this.page,
        limit: this.limit,
        shangjia_id: localStorage.getItem('userId')
      }
      this.$http.get(`${this.table}/page`, {params}).then(({data}) => {
        if (data && data.code === 0) {
          this.tableData = data.data.list
          this.total = data.data.total
        }
      })
    },
    handleSizeChange(val) {
      this.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    handleEdit(row) {
      this.$router.push(`/shangjia/${this.table}-manage?id=${row.id}`)
    },
    handleDelete(row) {
      this.$confirm('确认删除该商品?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.post(`${this.table}/delete`, {id: row.id}).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('删除成功')
            this.getList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>